import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * 
 */

/**
 * @author dan
 *
 */
public class Forum {

	/**
	 * 
	 */
	private String forumId;
	
	private String interactions;
	
	private TreeSet<String> users;

	private HashMap<String,ParticipationLog> plList = new HashMap<String, ParticipationLog>(); 
	
	public HashMap<String, ParticipationLog> getPlList() {
		return plList;
	}

	public void setPlList(HashMap<String, ParticipationLog> plList) {
		this.plList = plList;
	}

	public String getForumId() {
		return forumId;
	}

	public void setForumId(String forumId) {
		this.forumId = forumId;
	}

	public Forum() {
		// TODO Auto-generated constructor stub
	}

	public Forum(String name) {
		// TODO Auto-generated constructor stub
		forumId = name;
		this.interactions = "";
		this.users = new TreeSet<String>();
	}

	public String exportCSV(){
		String result = "";
		result +="User,No_Sent_Messages,No_Receive_Messages,No_Sent_Chars,No_Receive_Chars, No_Topics, No_Users_Receive, No_Users_Sent\n";
		for (Iterator it = this.plList.values().iterator(); it.hasNext();) {
			ParticipationLog log = (ParticipationLog) it.next();
			result	+=	log.exportCSV();
		}
		return result;
	}
	
	public void add(Utterance u){
		
		//check if it is existing user
		if( plList.containsKey(u.getUserId()) ){
			ParticipationLog log = plList.get(u.getUserId());
			log.addLog(u, true);
		}
		else{
			plList.put(u.getUserId(), new ParticipationLog(u,true));
		}
		
		//check if the ref user exist
		if( plList.containsKey(u.getRefUserId()) ){
			ParticipationLog log = plList.get(u.getRefUserId());
			log.addLog(u, false);
		}
		else{
			plList.put(u.getRefUserId(), new ParticipationLog(u,false));
		}
		
	}

	public String getInteractions() {
		return interactions;
	}

	public void setInteractions(String interactions) {
		this.interactions = interactions;
	}

	public TreeSet<String> getUsers() {
		return users;
	}

	public void setUsers(TreeSet<String> users) {
		this.users = users;
	}
	
	
	public void exportForum2UCINET(){
		
		String content="";
		content += "DL n="+this.getUsers().size()+"\n";
		content	+= "format = edgelist1 \n";
		content	+= "labels: \n";
		Integer i =0;
		String interactions = this.getInteractions();
		for (Iterator<String> it = users.iterator(); it.hasNext();) {
			String u = (String) it.next();
			i++;
			content += i+", ";
			interactions = interactions.replaceAll(u, i.toString());
			
		}
		content = content.substring(0,content.lastIndexOf(','));
		content += "\n";
		content += "data: \n";
		
		content	+= interactions;//this.getInteractions();
		content += "\n";
		
		//System.out.println(content);
    	ResultFileWriter outWriter		= new ResultFileWriter("ucinet_forum.txt", content);
    	outWriter.persist();
	}
	
}
